package com.xy.gateway.request.transquery;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.httpclient.ConnectTimeoutException;
import org.apache.commons.httpclient.params.HttpConnectionParams;
import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;

public class SSLCertSocketFactory implements SecureProtocolSocketFactory {

	/** Log object for this class */
	private SSLSocketFactory sf;

	/**
	 * Construct a basic SSLCertSocketFactory
	 * 
	 * @param caCert
	 *            the location of the x509 certificate for the CA
	 * @param clientCert
	 *            the location of the PKCS12 certificate of the client
	 */
	public SSLCertSocketFactory() {
		super();

		try {

			SSLContext sslContext = null;
			X509TrustManager xtm = new X509TrustManager() {
				public void checkClientTrusted(X509Certificate[] chain,
						String authType) {
				}

				public void checkServerTrusted(X509Certificate[] chain,
						String authType) {
				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			};
			try {
				sslContext = SSLContext.getInstance("TLS");
				X509TrustManager[] xtmArray = new X509TrustManager[] { xtm };
				sslContext.init(null, xtmArray,
						new java.security.SecureRandom());
			} catch (GeneralSecurityException gse) {
				// Print out some error message and deal with this exception
			}

			// Get socket factory
			sf = sslContext.getSocketFactory();
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}

	/**
	 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int,java
	 *      .net.I netAddress,int)
	 */
	public Socket createSocket(String host, int port, InetAddress clientHost,
			int clientPort) throws IOException, UnknownHostException {
		SSLSocket sslSocket = (SSLSocket) sf.createSocket(host, port,
				clientHost, clientPort);
		return sslSocket;
	}

	/**
	 * @see SecureProtocolSocketFactory#createSocket(java.lang.String,int)
	 */
	public Socket createSocket(String host, int port) throws IOException,
			UnknownHostException {
		SSLSocket sslSocket = (SSLSocket) sf.createSocket(host, port);
		return sslSocket;
	}

	/**
	 * @see SecureProtocolSocketFactory#createSocket(java.net.Socket,java.lang
	 *      .Strin g,int,boolean)
	 */
	public Socket createSocket(Socket socket, String host, int port,
			boolean autoClose) throws IOException, UnknownHostException {
		SSLSocket sslSocket = (SSLSocket) sf.createSocket(socket, host, port,
				autoClose);
		return sslSocket;
	}

	@Override
	public Socket createSocket(String arg0, int arg1, InetAddress arg2,
			int arg3, HttpConnectionParams arg4) throws IOException,
			UnknownHostException, ConnectTimeoutException {
		SSLSocket sslSocket = (SSLSocket) sf.createSocket(arg0, arg1, arg2,
				arg3);
		return sslSocket;
	}
}
